package cn.fastoo.web;

import cn.fastoo.common.dao.UserBaseDao;
import cn.fastoo.common.entity.UserAccount;
import cn.fastoo.common.entity.UserBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;


@Component
public class UserBaseArgumentsResolver implements HandlerMethodArgumentResolver {


	@Autowired
	private UserBaseDao userBaseDao;

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterType().equals(UserBase.class);
	}

	@Override
	public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {

			HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

			UserAccount userAccount = (UserAccount) request.getAttribute("userAccount");

			return  userBaseDao.findById(userAccount.getUserBaseId());
	}

}